<?php
/**
 * Created by PhpStorm.
 * User: ZhuXueSong
 * Date: 2019-07-01
 * Time: 09:46
 */

namespace App\Services\Third;

use App\Exceptions\ExceptionSms;
use App\Models\ThirdUse;
use App\Models\UserSms;

/**
 * 短信-掌榕网
 * Class SmsZRW
 * @package App\Services\Third
 */
class SmsZRW extends Base
{
    private $accessKey;
    private $secret;
    private $srvUrl = 'http://api.1cloudsp.com';

    const MOBILE_CODE_TEMPLATE = '252704'; // 验证码模板code

    const SMS_SING_USER = '【陌陌微】';

    /**
     * Sms constructor.
     * @param $accessKey
     * @param $secret
     */
    public function __construct($accessKey, $secret)
    {
        $this->accessKey = $accessKey; //Config::get('common.sms_key');
        $this->secret    = $secret;    //Config::get('common.sms_secret');
        parent::__construct($this->srvUrl, '80');
    }

    /**
     * 发送手机验证码
     * @param string $mobile
     * @param string $code
     * @param int $signType 签名类型
     * @return bool|\stdClass
     * @throws \Throwable
     */
    public function sendMobileCode($mobile, $code, $signType = UserSms::USER_SMS_TYPE)
    {
        $signName = self::SMS_SING_USER;
        return $this->sendSms($mobile, $code, self::MOBILE_CODE_TEMPLATE, $signName);
    }

    /**
     * 客户变更销售员短信通知
     * @param $mobile
     * @param $content
     * @param int $signType
     * @return bool|\stdClass
     * @throws \Throwable
     * @method：changeSaleInform
     * @author：wyk
     * @datetime：2022-10-13 15:43
     */
    public function changeSaleInform($mobile, $content, $signType = UserSms::USER_SMS_TYPE)
    {
        $signName = $signType == UserSms::USER_SMS_TYPE ? self::SMS_SING_USER : self::SMS_SIGN_WAITER;
        return $this->sendSms($mobile, $content, self::CHANGE_SALE_TEMPLATE, $signName);
    }

    /**
     * 发送手机验证码
     * @param $mobile
     * @param $code
     * @param $template
     * @param string $signName
     * @return bool|\stdClass
     * @throws \Throwable
     */
    private function sendSms($mobile, $code, $template, $signName)
    {
        $params = [
            'accesskey'  => $this->accessKey,
            'secret'     => $this->secret,
            'sign'       => $signName,
            'templateId' => $template,
            'mobile'     => $mobile,
            'content'    => $code,
        ];
        $result = $this->request('/api/v2/single_send', $params);
        if (isset($result['code']) && $result['code'] == "0") {
            return true;
        } else {
            ExceptionSms::returnError($result['msg'] ?? "");
            return false;
        }
    }
}
